// 引入 react 依赖
import React, { Component } from 'react';
import { connect } from 'react-redux';
import { Text, View, Image, TouchableOpacity, ImageBackground, ScrollView } from 'react-native';

// 引入第三方依赖
import _ from 'lodash';
import pureRender from 'pure-render-decorator';
import { Actions } from 'react-native-router-flux';

// antd 按需加载
import { Toast } from '@ant-design/react-native';

// 引入 处理数据 所需文件
import { subscription, unsubscription } from '../../module/module.js'

// 引入样式
import Styles from './style.js';

// 引入公用组件
import { } from '../../components/index.js'

// 引入工具包
import { size, ScreenWidth } from '../../util/utils.js'

class SubscribeBtn extends Component {

	static propTypes = {

	}

	constructor(props) {
		super(props)
		this.state = {
			whether: props.whether
		}
	}

	hanldeSubscription() {
		const { userInfo, exchangeId } = this.props
		this.setState({
			whether: 1
		})
		subscription({
			userId: userInfo.id,
			exchangeApiId: exchangeId
		}).then(res => {
			console.log(res)
			Toast.info('操作成功')
		}).catch(error => {
			this.setState({
				whether: 0
			})
		})
	}

	handleUnsubscription() {
		console.log(this.props)
		const { userInfo, exchangeId, id } = this.props
		this.setState({
			whether: 0
		})
		unsubscription({
			userId: userInfo.id,
			exchangeApiId: id
		}).then(res => {
			console.log(res)
			Toast.info('操作成功')
			// Toast.fail(res.message, 2)
		}).catch(error => {
			this.setState({
				whether: 1
			})
		})
	}

	render() {
		const { whether } = this.state

		if (whether) {
			return (
				<TouchableOpacity onPress={() => this.handleUnsubscription()} style={{ marginTop: 12, marginRight: 12 }}>
					<View style={{ paddingHorizontal: 8, paddingVertical: 3, borderWidth: .5, borderColor: '#8A90A8', borderRadius: 2, }}>
						<Text style={{ fontSize: 10, color: '#fff', lineHeight: 14, }}>已订阅</Text>
					</View>
				</TouchableOpacity>
			)
		} else {
			return (
				<TouchableOpacity onPress={() => this.hanldeSubscription()} style={{ marginTop: 12, marginRight: 12 }}>
					<View style={{ paddingHorizontal: 8, paddingVertical: 3, backgroundColor: '#7484FF', borderRadius: 2, }}>
						<Text style={{ fontSize: 10, color: '#fff', lineHeight: 14, }}>订阅</Text>
					</View>
				</TouchableOpacity>
			)
		}
	}
}

export default connect(({ userInfo, }) => ({
	userInfo,
}))(pureRender(SubscribeBtn))
